package com.oa.tools.jobs.impl;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.oa.tools.TkMapper;
import com.oa.tools.entity.FlowLog;
import com.oa.tools.entity.OAFlowInstanceWithBLOBs;
import com.oa.tools.jobs.FlowHandlerTaskBase;
import com.oa.tools.utils.DateUtil;
import com.oa.tools.utils.FileUtil;
import com.oa.tools.utils.MailUtil;
import com.oa.tools.utils.SysConfig;
import com.oa.tools.utils.http.EKPRequestClient;

import tk.mybatis.mapper.entity.Example;

public class FlowBlockMonitorTask extends FlowHandlerTaskBase{

	public FlowBlockMonitorTask(Map<String,TkMapper<?>> daoMap) {
		super(daoMap);
	}

	@Override
	public String exec(boolean notice, OAFlowInstanceWithBLOBs obj) throws Exception {
		return null;
	}

	@Override
	public String start(boolean notice,boolean force, long lastestDateMill) throws Exception {
		Example example_log = new Example(FlowLog.class);
		example_log.createCriteria().andEqualTo("fdStatus",FLOW_STATUS_ING).andLessThan("fdStartDate", new Date(System.currentTimeMillis()-1000L*60*60*24*5));
		List<FlowLog> flowLogs = this.flowLogDAO.selectByExample(example_log);
		StringBuilder result = new StringBuilder();
		if(flowLogs==null || flowLogs.size()==0) {
			return null;
		}else {
			for(FlowLog flowLog:flowLogs) {
				String nodeName = flowLog.getFdFactNodeName();
				String nodeId = flowLog.getFdFactNodeId();
				String flowId = flowLog.getFdProcessId();
				
				String recordPath = SysConfig.getRecordPath()+"/"+this.getClass().getName()+"_"+flowId+".txt";
				if(new File(recordPath).exists()) {
					continue;
				}
				
				OAFlowInstanceWithBLOBs flowObj = this.oAFlowInstanceDAO.selectByPrimaryKey(flowId);
				
				StringBuilder sb = new StringBuilder();
				
				sb.append("\r\n流程ID:");
				sb.append(flowId);
				sb.append("\r\n流程主题:");
				sb.append(flowObj==null?"未找到流程":flowObj.getDocSubject());
				sb.append("\r\n阻塞节点ID:");
				sb.append(nodeId);
				sb.append("\r\n阻塞节点名称:");
				sb.append(nodeName);
				sb.append("\r\n阻塞开始时间:");
				sb.append(DateUtil.date2Str(flowLog.getFdStartDate()));
				if(flowObj!=null) {
					sb.append("\r\n查看详情:");
					sb.append("<a href='http://oa.fengbangleasing.com/km/review/km_review_main/kmReviewMain.do?method=view&fdId="+flowId+"' >"+flowObj.getDocSubject()+"</a>");
				}
				sb.append("\r\n");
				
				

				
				sb.append("\r\n------------------------------------------------------------------------\r\n");
				FileUtil.writeTxt2File(sb.toString(), recordPath);
				
				
				//http://oa.fengbangleasing.com/sys/common/dataxml.jsp?s_bean=lbpmHandlerParseService&modelId=1645ae3c609a034ee1b9f4d41b7af75e&modelName=com.landray.kmss.km.review.model.KmReviewMain&analysis4View=false
				
					if(flowObj!=null) {
						if(flowObj.getDocStatus().equals(FLOW_STATUS_DRAFT)) {
							
						}else if(flowObj.getDocStatus().equals(FLOW_STATUS_DISCARD)) {
							
						}else {
							Map<String,String> nodeHandlerMap = EKPRequestClient.getFlowHandleFromEkp(flowId);
							String handlers = nodeHandlerMap.get(nodeId);
							if(handlers==null || handlers.contains("岗位成员未设置") || handlers.contains("暂时无法计算处理人")) {
//								String[]handler_arr = handlers.split(";");
//								for(String handler :handler_arr) {
//									if(handler.indexOf('(')>0) {
//										handler = handler.substring(handler.indexOf('(')+1,handler.indexOf('('));
//									}
//								}
							
								result.append("\r\n阻塞处理人:"+handlers);
								result.append(sb);
							}
						}
					}
				}
		}
		
		if(result.length()>0) {
			String content = "<pre>Dear sushaolei "+result.toString()+"\r\n请知晓确认</pre>";
			String [] receiveMailAccounts = SysConfig.getSysSet("管理员接收邮箱").toArray(new String[0]);
			MailUtil.sendMail(receiveMailAccounts,"流程阻塞", content);
		}
		
		return result.toString();
	}

	
}
